<?php

/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 15/8/17
 * Time: 下午2:10
 */
class PubGame
{
    // 坑爹的ETL游戏转换
    public static function getDataGameSrc($gameSrc){
        $map = array(
            'slg'    => 'legion',
            'st'     => 'stormthron',
            'winter' => 'Winterfrost Legac',
            'hotb'   => 'htb',
        );

        return isset($map[$gameSrc]) ? $map[$gameSrc] : $gameSrc;
    }

    public static function getDefaultPlatform($platformParams = 'p', $type=null){
        if(isset($_REQUEST[$platformParams])) return $_REQUEST[$platformParams];

        $platforms = PubAcc::getMyPlatformList();
        if(!empty($platforms)){
            if(isset($type)){
                foreach($platforms as $k=>$v){
                    if($v['PlatformType'] == $type){
                        return $k;
                    }
                }
            }else{
                return key($platforms);
            }
        }else{
            return 'r2games';
        }
    }

    public static function getDefaultGame($platform, $gameParams = 'g'){
        if(isset($_REQUEST[$gameParams])) return $_REQUEST[$gameParams];

        if(empty($platform) || PubAcc::getStateByPlatform($platform)) return '';

        $gameList = array();
        $gameInfo = PubAcc::getGame();
        foreach($gameInfo['Details'] as $v){
            if($v['PlatformID'] == $platform){
                $gameList[$v['GameSrc']] = $gameInfo['Game'][$v['GameSrc']];
            }
        }

        if(empty($gameList)){
            return '';
        }else{
            return key($gameList);
        }
    }

    // 获得GameSrc => GameID 的MAP表
    private static function getGameIDMap(){
        $gameArr = self::getOssGame(false);
        $map = array();
        foreach($gameArr as $v){
            $key = empty($v['data_flag']) ? $v['game'] : $v['data_flag'];
            $map[$key] = $v['id'];
        }
        return $map;
    }

    // 获得SID
    private static function getServerIDMap($gameId){
        $serverArr = self::getOssServer($gameId, false);
        $map = array();
        foreach($serverArr as $k=>$v){
            $map['S'.$v['servernum']] = $k;
        }
        return $map;
    }

    // 将GameSrc转成GameID
    public static function getOssGameID($gameSrc, $sys=null){
        if(empty($gameSrc)) return 'ALL';
        
        $map = array(
        		'stormthron' => 'stormthrone',
        		'Winterfrost Legac' => 'winter',
        		'htb' => 'hotb'
        );
        
        if($sys == 'pl' && key_exists($gameSrc, $map)){
        	$gameSrc = $map[$gameSrc];
        }
        

        $gameIdMap = self::getGameIDMap();
        $return = isset($gameIdMap[$gameSrc]) ? $gameIdMap[$gameSrc] : -1;
        
        return $return;
    }

    // 获得服的ServerID
    public static function getOssServerID($gameId, $server){
        $server = strtoupper($server);
        if(empty($server) || $server == 'ALL') return 'ALL';

        $serverIdMap = self::getServerIDMap($gameId);
        return isset($serverIdMap[$server]) ? $serverIdMap[$server] : -1;
    }

    // 从OSS获取游戏信息
    public static function getOssGame($option = true){
        $res = PubFunc::api('getGame', array(), 'oss');

        $options = array();
        foreach($res as $key => $val) {
            $options[$val['id']] = $option ? $val['name'] : $val;
        }

        return $options;
    }

    // 从OSS获取服信息
    public static function getOssServer($gameid = '', $option = true){
        $res = PubFunc::api('getServer', array('game' => $gameid), 'oss');

        $options = array();

        foreach($res as $key => $val){
            if(is_array($val)){
                $options[$key] = $option ? $val['servername'] : $val;
            }
        }

        return $options;
    }

    public static function getGameForSel($platform){
        $plgame   = PubAcc::getGame();
        $details  = $plgame['Details'];
        $GameList = array();

        if($platform == 'ALL'){
            //获取GameID=>GameSrc
            foreach ($plgame['Game'] as $val){
                $kvL[$val['GameID']] = array('GameSrc' => $val['GameSrc'] , 'GameName' => self::getGameName($val));
            }

            $allPlGame = PubAcc::getStateByGame();
            $AllGame   = true;

            foreach($allPlGame as $key => $val){
                if($val === true){
                    $GameList[$kvL[$key]['GameSrc']] = self::getGameName($kvL[$key]);
                }else{
                    $AllGame = false;
                }
            }

        }else{
            foreach($details as $val){
                if($val['PlatformID'] == $platform){
                    $GameList[$val['GameSrc']] = self::getGameName($val);
                }
            }
            $AllGame = PubAcc::getStateByPlatform($platform);
        }

        // todo:暂时屏蔽其他游戏
//        $arr = PubGame::getDataGame();
//        foreach($GameList as $k=>$v){
//            if(!in_array($k, $arr)){
//                unset($GameList[$k]);
//            }
//        }

        if($AllGame)
            $GameList = array_merge(array('' => 'ALL'), $GameList);

        return $GameList;
    }

    // 获得游戏名称(中、英)
    private static function getGameName($val){
        return (empty($val['GameNameEn']) ? $val['GameName'] : $val['GameNameEn']);
    }
}